// pages/user/getPhone/getPhone.js
const handler = require("../../../utils/function.js");
var api = require("../../../api.js"),
    app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        getPhoneAndYanZhengMa: true,
        sendtxt: "获取验证码",
        phone: null,
        phone: '',
        verificationcode: '',
        sendtxt: '发送验证码',
        gettingCode: true,
        successCallback: null
    },
    // phoneinput 获取电话号码
    phoneinput: function(event) {
        var that = this
        that.setData({
            phone: event.detail.value
        })
         wx.setStorageSync("phonep", event.detail.value);
    },
    //获取验证码
    phonecode: function(event) {
        var that = this
        that.setData({
            verificationcode: event.detail.value
        })
    },
    // 发送验证码
    sendcode: function() {
        var that = this;
        if (!that.data.gettingCode) {
            return;
        }
        if (that.data.phone == '' || !/^1\d{10}$/.test(that.data.phone)) {
            handler.showToast('请输入正确的电话号码格式', "none")
            return;
        }
        wx.showLoading({})
        // console.log('触发发验证码')
        app.request({
            url: api.user.check_phone,
            method: 'post',
            data: {
                phone: that.data.phone,
            },
            success: function(rs) {
              wx.hideLoading()
                if (rs.code == 1) {
                    handler.showToast('该手机号已绑定账号', "none")
                    return;
                } else {
                    app.request({
                        url: api.user.send_sms,
                        method: 'post',
                        data: {
                            phone: that.data.phone,
                        },
                        success: function(rss) {
                            if (rss.code == 0) {
                                handler.showToast('验证码获取失败', "none")
                                return;
                            } else {
                                that.countDown();
                            }
                        }
                    })
                }
            }
        })
    },
    //倒计时 倒数
    countDown: function() {
        var that = this;
        that.data.gettingCode = false;
        this.setData({ gettingCode:false});
        var seconds = 59;
        var countTimer = setInterval(function() {
            that.setData({
                sendtxt: seconds + "s"
            })
            seconds--;
            if (seconds <= 0) {
                countTimer = clearInterval(countTimer);
                that.setData({
                    sendtxt: '获取验证码'
                })
                that.data.gettingCode = true;
              that.setData({ gettingCode:true });
            }
        }, 1000);
    },
    // getPhoneLogin 登录的电话号码
    getPhoneLogin: function(e) {
        var that = this;
        // 登录
        wx.showLoading({
        })
        app.request({
            url: api.user.binding_phone, //绑定手机号
            method: 'post',
            data: {
                phone: that.data.phone,
                code: that.data.verificationcode,
                access_token: wx.getStorageSync("access_token"),
            },
            success: function(rs) {
                console.log(rs)
                wx.hideLoading()
                if (rs.code == 0) {
                    handler.showToast(rs.msg, "none")
                    that.data.gettingCode = true;
                  that.setData({ gettingCode: true });
                    return;
                } else {
                    that.data.gettingCode = false;
                  that.setData({ gettingCode: false });
                  handler.showToast('登陆成功');
                  wx.reLaunch({
                    url: '/pages/map/driverServer/driverServer',
                  })
                }
            }
        })
        if (!this.data.gettingCode) {
            // handler.showToast('登录成功', 'none')
            // wx.setStorageSync("userPhone", this.data.phone)
            // wx.reLaunch({
            //   url: '/pages/map/driverServer/driverServer'
            // })
            // handler.intoPageHandler("../../map/driverServer/driverServer");
        }
    }
})